package dnl.util.ui.tree;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;

import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeCellRenderer;


/**
 * A Simple cell renderer for the tree.
 *
 * @author Daniel Or
 * @version 1.0
 */
class CellRenderer extends JLabel implements TreeCellRenderer {

  //~ Constructors =============================================================

  /**
   * Constructor for CellRenderer.
   */
  public CellRenderer() {
    super();
    textSelectionColor = UIManager.getColor("Tree.selectionForeground");
    textNonSelectionColor = UIManager.getColor("Tree.textForeground");
    bkSelectionColor = UIManager.getColor("Tree.selectionBackground");
    bkNonSelectionColor = UIManager.getColor("Tree.textBackground");
    borderSelectionColor = UIManager.getColor("Tree.selectionBorderColor");
    setOpaque(false);
  }

  //~ Methods ==================================================================

  /**
   * @see javax.swing.tree.TreeCellRenderer#getTreeCellRendererComponent(javax.swing.JTree,
   *      java.lang.Object, boolean, boolean, boolean, int, boolean)
   */
  @Override
public Component getTreeCellRendererComponent(JTree tree, Object value,
    boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;

    if (node.getUserObject() instanceof TreeItem) {
      TreeItem item = (TreeItem) node.getUserObject();

      if (expanded && (item.getExpandedIcon() != null)) {
        setIcon(item.getExpandedIcon());
      } else if (item.getIcon() != null) {
        setIcon(item.getIcon());
      }
    } 
    
    if (node.getUserObject() instanceof Boolean) {
      setText("Retrieving data...");
    }
    else 
			setText(String.valueOf(node.getUserObject()));

    setFont(tree.getFont());
    setForeground(sel ? textSelectionColor : textNonSelectionColor);
    setBackground(sel ? bkSelectionColor : bkNonSelectionColor);
    selected = sel;
    return this;
  }

  /**
   * @param g
   */
  @Override
public void paintComponent(Graphics g) {
    Color bColor = getBackground();
    Icon icon = getIcon();
    g.setColor(bColor);
    int offset = 0;

    if ((icon != null) && (getText() != null)) {
      offset = (icon.getIconWidth() + getIconTextGap());
    }

    g.fillRect(offset, 0, getWidth() - 1 - offset, getHeight() - 1);

    if (selected) {
      g.setColor(borderSelectionColor);
      g.drawRect(offset, 0, getWidth() - 1 - offset, getHeight() - 1);
    }

    super.paintComponent(g);
  }

  //~ Instance variables =======================================================

  protected boolean selected;
  protected Color textSelectionColor;
  protected Color textNonSelectionColor;
  protected Color bkSelectionColor;
  protected Color bkNonSelectionColor;
  protected Color borderSelectionColor;
}
